<template>
  <BasicForm @register="registerForm"/>
</template>

<script lang="ts" setup>
  import {BasicForm, useForm} from '/@/components/Form';
  import {baseInfoFormSchema} from '../../codeGen.data';
  import { toRefs, watch, defineProps, defineExpose} from 'vue';
  const props = defineProps({
    // 子组件接收父组件传递过来的值
    genTable: Object,
  })

  const {genTable} = toRefs(props);
  const [registerForm, {resetFields, setFieldsValue, validate: validateForm}] = useForm({
    schemas: baseInfoFormSchema,
    showActionButtonGroup: false,
    baseColProps: {lg: 10, sm: 24} 
  });

  watch(
    () => props.genTable,
    async  (val) => {
      try {
        if (genTable) {
          // 重置表单
          await resetFields();
          //表单赋值
          await setFieldsValue({
            ...val,
          });
        }
      } catch(err) {
        console.error(err)
      }
    },
    { deep: true, immediate: true }
  )

  const getData = async () => {
    return await validateForm();
  }

  //关键点 把 方法暴露给父组件
  defineExpose({getData})

</script>